def list2tree(data: list, index: int = None) -> list:
    """列表转树函数-非递归方法"""
    mapping: dict = dict(zip([i['id'] for i in data], data))
    container: list = []
    for d in data:
        parent: dict = mapping.get(str(d['parent']))
        if parent == index:
            container.append(d)
        else:
            children: list = parent.get('children')
            if not children:
                children = []
            children.append(d)
            parent.update({'children': children})
    return container


def list_to_tree(data: list, index: int = None):
    """列表转树函数-递归方法"""
    container: list = []
    for x in data:
        if (str(x['parent']) == index):
            x['children'] = list_to_tree(x['id'], data)
            container.append(x)
    return container